﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security.Cryptography;

public partial class ThayDoiMatKhau : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["taikhoan"] == null)
        {
            Response.Redirect("Dangnhap.aspx");
        }
        else
        {
            lblTaiKhoan.Text = Session["taikhoan"].ToString();
        }
    }

    protected void btBoQua_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }

    public int KiemtraTaiKhoan(string id, string pass)
    {

        pass = MaHoaMatKhau(pass);
        ConnectionData conn = new ConnectionData();
        string sql = "select * from khach_hang where tai_khoan = '" + id + "' and mat_khau = '" + pass + "'";

        if (conn.LayDangNhap(sql) == 1)
            return 1;
        else
            return 0;
    }


    public string MaHoaMatKhau(string password)
    {
        UnicodeEncoding encoding = new UnicodeEncoding();
        Byte[] hashBytes = encoding.GetBytes(password);
        // Compute the SHA-1 hash
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        Byte[] cryptPassword = sha1.ComputeHash(hashBytes);
        return BitConverter.ToString(cryptPassword);
    }

    protected void btThayDoi_Click(object sender, EventArgs e)
    {
        if (txtMatKhauCu.Text == "" || txtMatKhauMoi.Text == "" || txtXacNhan.Text == "")
        {
            lblThongbao.Text = "Thông tin tài khoản chưa đầy đủ";
        }
        else
        {

            string TaiKhoan, MatKhauCu, MatKhauMoi, XacNhan;
            TaiKhoan = Session["taikhoan"].ToString();
            MatKhauCu = txtMatKhauCu.Text;
            MatKhauMoi = txtMatKhauMoi.Text;
            XacNhan = txtXacNhan.Text;
            if (KiemtraTaiKhoan(TaiKhoan,MatKhauCu)==0)
            {
                lblThongbao.Text = "Mật Khẩu Cũ Không Đúng";
            }

            else if (txtMatKhauMoi.Text != txtXacNhan.Text)
            {
                lblThongbao.Text = "Xác Nhận Mật Khẩu Không Đúng";
                //Response.Redirect("ThayDoiMatKhau.aspx");
            }
            else
            {
                MatKhauMoi = MaHoaMatKhau(MatKhauMoi);
                String sql = "Update khach_hang set mat_khau = '" + MatKhauMoi + "' where tai_khoan = '" + TaiKhoan + "'";
                ConnectionData cn = new ConnectionData();
                if (cn.ExecuteSQL(sql) == 1)
                {
                    Response.Redirect("ThayDoiMatKhauThanhCong.aspx");
                }
                else
                {
                    Response.Redirect("ThayDoiMatKhau.aspx");
                    lblThongbao.Text = "Cập nhật thất bại";
                }
            }
        }
    }
}